package com.cykj.page04;

import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * @BelongsProject: HandTomcat
 * @BelongsPackage: com.cykj.page04
 * @Author: 阿隆
 * @CreateTime: 2025-10-29 19:47
 * @Description: 类对象去操作成员方法
 * @Version: 1.0
 */
public class Lesson04 {
    public static void main(String[] args) {

        try {
            Class<?> cls = Class.forName("com.cykj.page04.Student");

            Student stu = (Student) cls.newInstance();

            //获取所有的方法(public)  包括父类公有的方法
            Method[] methods = cls.getMethods();

            //获取所有的方法 (private)  不包括的方法
            Method[] declaredMethods = cls.getDeclaredMethods();


            Method method = cls.getMethod("eat");

            method.invoke(stu);

            Method playing = cls.getMethod("playing", String.class);

            playing.invoke(stu, "篮球");


            Method sleep = cls.getDeclaredMethod("sleep");
            sleep.setAccessible(true);
            sleep.invoke(stu);


            System.out.println(Arrays.toString(declaredMethods));

            System.out.println("=========================================");

            System.out.println(Arrays.toString(methods));

        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}